<?php

class BaseAction extends Action
{
    // 站点名称
    protected $_title;
    // 分页使用
    protected $_page;
    protected $_item_count;
    // 来源类型
    protected $_from_type;
    protected $_type_wap;
    // 关注状态
    protected $_follow_status;
    // 当前URL
    protected $_self_url;
    // 用户信息
    public $profile;

    protected $api_brand = 'https://base.zhuritec.com:8766/api/yibum/p/brand/';
    protected $api_brand_list = 'https://base.zhuritec.com:8766/api/yibum/p/brand/getBrandBychannelName?name=时尚圈';
    protected $api_designer = 'https://base.zhuritec.com:8766/api/yibum/p/designer/';
    protected $api_designer_list ='https://base.zhuritec.com:8766/api/yibum/p/designer/page';
    protected $api_series = 'https://base.zhuritec.com:8766/api/yibum/p/brandAtlas/getByBrandAtlasById?atlasId=';
    protected $api_series_all_list = 'https://base.zhuritec.com:8766/api/yibum/p/brandAtlas/getAllAtlasBychannelName?name=时尚圈';
    protected $api_series_brand_list = 'https://base.zhuritec.com:8766/api/yibum/p/brandAtlas/getByBrandId';


    public function _initialize()
    {
        $data = [];
        if (!$this->mid) {
            $publicAccess = include APPS_PATH.'/w3g/Conf/access.inc.php';
            $publicAccess = $publicAccess['access'];
            // _ajax 参数是为了区别于老的html页面，兼容老系统用  2020-12-14 gujian
            if(  $_GET['_ajax'] == 1){
                //token判断: 需要登录的页面做token验证 2020-12-14 gujian
                if(!isset($_GET['token'])){
                    $this->ajaxReturn([],'token必须',101);
                }else{
                    if( !empty($_GET['token']) ){
                        $rs = model('UserToken')->where( ['token_value'=> trim($_GET['token']) ] )->find();
                        if(!$rs){
                            if(MODULE_NAME  != 'Fashion'){
                                exit( json_encode( ['status'=>106 , 'msg'=> 'token is invalid!'] ,JSON_UNESCAPED_UNICODE)   );
                            }
                        }
                        if(MODULE_NAME  != 'Fashion'){
                            if( $rs['token_value'] + 3600 *24 < time()){
                                $this->ajaxReturn($data,'请重新登录',105);
                            }
                            if($rs && $rs['uid']){
                                $this->uid = $rs['uid'] ;
                            }
                        }

                    }else{
                        if(MODULE_NAME  != 'Fashion'){
                            $need_login = model('Passport')->needLogin();
                            if($need_login){
                                $this->ajaxReturn([],'请登录',101);
                            }
                        }

                    }
                }

            }else if( ACTION_NAME !='index' ){
                if ($publicAccess[APP_NAME.'/'.MODULE_NAME.'/'.ACTION_NAME] === true
                        || $publicAccess[APP_NAME.'/'.MODULE_NAME.'/*'] === true
                        || $publicAccess[APP_NAME.'/*/*'] === true) {
                    redirect(U('w3g/Public/login'));
                    exit();
                }
            }
        }
        global $ts;

        $this->_title = $ts ['site'] ['site_name'];
        $this->assign('site_name', $this->_title);

        // 分页
        $_GET ['page'] = $_POST ['page'] ? intval($_POST ['page']) : intval($_GET ['page']);
        $this->_page = $_GET ['page'] > 0 ? $_GET ['page'] : 1;
        $this->assign('page', $this->_page);
        $this->_item_count = 10;
        $this->assign('item_count', $this->_item_count);

        // 来源类型
        // if(is_iphone()){
        // $this->_type_wap = 3;
        // }elseif(is_android()){
        // $this->_type_wap = 2;
        // }else{
        $this->_type_wap = 1;
        // }

        $this->_from_type = array(
                '0' => '网站',
                '1' => 'H5版本',
                '2' => 'Android客户端',
                '3' => 'iPhone客户端',
        );
        $this->assign('from_type', $this->_from_type);

        // 关注状态
        $this->_follow_status = array(
                'eachfollow' => '相互关注',
                'havefollow' => '已关注',
                'unfollow' => '未关注',
        );
        $this->assign('follow_status', $this->_follow_status);

        // 当前URL
        $this->_self_url = 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER ['REQUEST_URI'];
        if (isset($_POST ['key'])) {
            $this->_self_url .= "&key={$_POST['key']}";
            $this->_self_url .= isset($_POST ['user']) ? '&user=1' : '&weibo=1';
        }
        $this->assign('self_url', $this->_self_url);

        // 是否为owner
        $this->assign('is_owner', ($this->uid == $this->mid) ? '1' : '0');

        $data ['uid'] = $this->uid;
        //$data ['page'] = $this->_page;

        // 用户资料
        $user_res = m('user')->where($data)->find();
        $this->profile = $user_res;
        $this->assign('profile', $this->profile);

        $logo = '';
        if (($logo = model('Xdata')->get('admin_Mobile:w3gLogo'))) {
            $logo = getAttachUrlByAttachId($logo['logo']);
        }
        $this->assign('logo', $logo);
    }
    public function need_login()
    {
        // 登录验证
        $passport = model('Passport');
        if (!$passport->isLogged()) {
            $_SESSION ['__forward__'] = $_SERVER ['REQUEST_URI'];
            redirect(U('wap/Public/login'));
        }
    }
    public function success($msg, $delay = 0, $url = '')
    {
        if (IS_AJAX) {
            echo $msg;
            exit();
        }
        if (!$url) {
            $url = $_SERVER ['HTTP_REFERER'];
        }

        if (!empty($msg) && $delay == 0) {
            $delay = 3;
        }

        redirect($url, $delay, $msg);
    }
    public function error($msg, $delay = 0, $url = '')
    {
        $this->success($msg, $delay, $url);
    }

    protected function http_request($url, $data = null, $headers = array())
    {
        $curl = curl_init();
        if (count($headers) >= 1) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
